package com.easy.controller;

import com.easy.bean.User;
import com.easy.service.UserService;
import com.easy.util.Page;
import com.easy.util.PasswordUtil;
import com.easy.util.Result;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin//允许跨域
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    //增
    @PostMapping
    public Result add(@RequestBody User user){
        Object obj = userService.add(user);
        return Result.success("保存成功",obj);
    }

    //改
    @PutMapping
    public Result edit(@RequestBody User user){
        user.setPassword(PasswordUtil.getEncryption(user.getPassword()));
        Object obj = userService.edit(user);
        return Result.success("编辑成功",obj);
    }

    @PutMapping("/password")
    public Result editPassword(@RequestBody User user){

        Object obj = userService.editPassword(user);
        return Result.success("修改密码成功",obj);
    }

    //删
    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id){
        Object obj = userService.del(id);
        return Result.success("删除成功",null);
    }

    //查
    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id){
        Object obj = userService.one(id);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all(User user){
        Object obj = userService.all(user);
        return Result.success(obj);
    }

    @GetMapping("page")
    public Result page(Page page,User user){
        //查询一共多少条数据
        //查询当前页面要显示的数据
        //将以上数据封装到page对象中
        userService.page(page,user);
        //返回page对象
        return Result.success(page);
    }
}
